package com.wx.md.controller;

import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.wx.md.common.RedisService;
import com.wx.md.enums.ResponseCode;
import com.wx.md.exception.BusinessException;
import com.wx.md.tb.SysUser;
import com.wx.md.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Api(value = "用户登陆/登出", tags = "用户登陆/登出相关的接口")
@RestController
@RequestMapping("/admin")
public class LoginController extends ApiController {


    @Autowired
    LoginService loginService;

    @Autowired
    RedisService redisService;


    /**
     * 登陆
     *
     * @param sysUser
     * @return
     */
    @ApiOperation(value = "用户登陆", notes = "用户登陆的接口")
    @PostMapping("/login")
    public R<SysUser> login(@RequestBody  SysUser sysUser) {
        return loginService.login(sysUser);
    }


    @GetMapping("/info")
    public R info(@RequestHeader("authorization") String token) {
        SysUser sysUserDb = loginService.info(token);
        //{"code":20000,"data":{"roles":["admin"],"introduction":"I am a super administrator","avatar":"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif","name":"Super Admin"}}
        if(sysUserDb==null){
            throw new BusinessException(ResponseCode.TOKEN_PAST_DUE);
        }
        HashMap resultMap = new HashMap();
        resultMap.put("name",sysUserDb.getUserName());
        resultMap.put("introduction","用户描述");
        resultMap.put("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
        List<String> roles = new ArrayList<>();
        roles.add("editor");
        resultMap.put("roles",roles);

        return R.ok(resultMap);
    }

    /**
     * 登出
     *
     * @return
     */
    @ApiOperation(value = "用户登出", notes = "用户登出的接口")
    @GetMapping("/logout")
    public R<String> logout(@RequestHeader("authorization") String token) {
        loginService.logout(token);
        return R.ok("退出成功");
    }

}
